﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace FastLibrary.RSS
{
    public static class RSSReader
    {
        static List<RSS> createList(DataTable table, int size)
        {
            List<RSS> list = new List<RSS>();
            foreach (DataRow item in table.Rows)
            {
                list.Add(new RSS()
                {
                    title = item["title"].ToString(),
                    date = DateTime.Parse(item["pubdate"].ToString()).ToShortDateString(),
                    link = item["link"].ToString(),
                    description = item["description"].ToString()
                });
            }

            if (size.Equals(0))
                return list;
            else
                return list.Take(size).ToList();
        }

        /// <summary>
        /// Get the list of items in RSS url
        /// </summary>
        /// <param name="url">Url of RSS</param>
        /// <param name="size">Number of items to return. Put 0 if you wanna bring all items</param>
        /// <returns>The list of items RSS</returns>
        public static List<RSS> GetRSSList(string url, int size)
        {
            DataSet m_ds = null;
            System.Xml.XmlTextReader oXml = new System.Xml.XmlTextReader(url);
            oXml.WhitespaceHandling = System.Xml.WhitespaceHandling.None;
            System.Text.StringBuilder oBuilder = new StringBuilder();


            bool fContinue = oXml.Read();
            while (fContinue)
            {
                string sName = oXml.Name.Replace(":", "_");
                if (oXml.NodeType == System.Xml.XmlNodeType.Element)
                {
                    //Add the element... 
                    oBuilder.Append("<" + sName);
                    if (oXml.HasAttributes)
                    {
                        while (oXml.MoveToNextAttribute())
                        {
                            sName = oXml.Name.Replace(":", "_");
                            oBuilder.AppendFormat(" " + sName + "=\"" + oXml.Value + "\"");
                        }
                    }
                    oBuilder.Append(">");


                }
                else if (oXml.NodeType == System.Xml.XmlNodeType.Text)
                {
                    oBuilder.Append(System.Web.HttpUtility.HtmlEncode(oXml.Value));
                }
                else if (oXml.NodeType == System.Xml.XmlNodeType.EndElement)
                {
                    oBuilder.Append("</" + sName + ">");
                }

                fContinue = oXml.Read();
            }
            oXml.Close();

            string sXmlResult = oBuilder.ToString();


            m_ds = new DataSet();
            System.IO.StringReader oStringReader = new System.IO.StringReader(sXmlResult);
            try
            {
                m_ds.ReadXml(oStringReader, System.Data.XmlReadMode.Auto);
            }
            catch (Exception)
            {
                m_ds = null;
            }
            if (oStringReader != null)
                oStringReader.Close();
            if (oXml != null)
                oXml.Close();

            return createList(m_ds.Tables[2], size);
        }
    }
}
